//============================================================================
// Name        : main.cpp
// Author      : Benedikt Bovelette / Ingmar Hartung
// Version     :
// Copyright   : (c) 2012
// Description :
//============================================================================
#include "Header/main.h"
#include "Header/cylindercg.h"
#include "Header/oval.h"
#include "Header/wings.h"

float stepSize = 3.0f;
GLuint texture[1];
// angle of rotation for the camera direction
float angle = 0.0;
// actual vector representing the camera's direction
float lx = 0.0f, lz = -1.0f;
// XZ position of the camera
float x = 0.0f, z = 5.0f;

float g_RotateX = 0.0f;
float g_RotationSpeed = 0.1f;

void drawWindRad(float x, float y, float z) {
	glTranslatef(x, y, z);

	glRotatef(y_angle, 0.0, 1.0, 0.0);
	glRotatef(x_angle, 1.0, 0.0, 0.0); // Set x_angle angle
	DrawOval();
	glDisable(GL_LIGHT1);
	DrawWings();
	DrawCylinderOnTop();
	DrawCylinder();
	DrawCylinderOnButtom();

}

void ground(float x, float y, float z) {
	glRotatef(y_angle, 0.0, 1.0, 0.0);
	glRotatef(x_angle, 1.0, 0.0, 0.0); // Set x_angle angle
	glScalef(x, y, z);
	glutSolidCube(1.1);

}
void display(void) {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	// left - right and zoom in and zoom out
	gluLookAt(x, 1.0f, z, x + lx, 1.0f, z + lz, 0.0f, 1.0f, 0.0f);

	drawWindRad(-1.0, 1.0, -15.0);
//	drawWindRad(-0.9, 1.5, -10.5);
//	drawWindRad(-0.8, 2.0, -6.0);

	ground(100.0, 0.01, 100.0);

	glFlush();
	glutSwapBuffers();
}

void init(void) {
	glClearColor(0.4f, .3f, 1.0f, 0.5f); //Blau-rot
//	glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //schwarz
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_COLOR_MATERIAL);
	glColorMaterial(GL_FRONT, GL_AMBIENT);
	glEnable(GL_LIGHTING);
	glEnable(GL_NORMALIZE);

	// Farben der Lichtquelle setzen (r,g,b,a)
	GLfloat ambient[4] = { 0.23f, 0.23f, 0.23f, 1.0f };
	GLfloat diffuse[4] = { 0.28f, 0.28f, 0.28f, 1.0f };
	GLfloat specular[4] = { 0.77f, 0.77f, 0.77f, 1.0f };

	glEnable(GL_LIGHT0);
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, specular);

}

void reshape(int w, int h) {
	double aspectRatio;

	// Define the portion of the window used for OpenGL rendering.
	glViewport(0, 0, w, h); // View port uses whole window

	// projection view matrix
	w = (w == 0) ? 1 : w;
	h = (h == 0) ? 1 : h;
	aspectRatio = (double) w / (double) h;
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(15.0, aspectRatio, 10.0, 50.0);
}

void motion(void) {
	if (left == TRUE) {
		y_angle += stepSize;
		if (y_angle > 180.0f) {
			y_angle -= 360.0f;
		}
	} else if (right == TRUE) {
		y_angle -= stepSize;
		if (y_angle < -180.0f) {
			y_angle += 360.0f;
		}
	}
	glutPostRedisplay();
}

void mySpecialKeyFunc(int key, int xx, int yy) {
	float fraction = 0.1f;
	switch (key) {
	case GLUT_KEY_LEFT:
		angle -= 0.01f;
		lx = sin(angle);
		lz = -cos(angle);
		break;
	case GLUT_KEY_RIGHT:
		angle += 0.01f;
		lx = sin(angle);
		lz = -cos(angle);
		break;
	case GLUT_KEY_UP:
		x += lx * fraction;
		z += lz * fraction;
		break;
	case GLUT_KEY_DOWN:
		x -= lx * fraction;
		z -= lz * fraction;
		break;
	case GLUT_KEY_PAGE_UP:
		y_angle += stepSize;
		if (y_angle > 180.0f) {
			y_angle -= 360.0f;
		}
		break;
	case GLUT_KEY_PAGE_DOWN:
		y_angle -= stepSize;
		if (y_angle < -180.0f) {
			y_angle += 360.0f;
		}
		break;
	}

	glutPostRedisplay();
}

int main(int argc, char **argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_STENCIL);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("Windkraftanlage Bovelette / Hartung");
	glutSpecialFunc(mySpecialKeyFunc);

	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);

	//glutKeyboardFunc(keyboard);

	// This is the special thing for this lesson
	// We tell Glut to call our function every time he can
	glutIdleFunc(animate);

	glutMainLoop();
	return 0;
}
